package jenry.testcp;

import java.util.ArrayList;
import java.util.HashMap;

import jenry.testcp.FirstProviderMetaData.UserTableMetaData;
import android.app.ListActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Gan_TestCP extends ListActivity {
	
	private Button myInsert = null;
	private Button myQuery = null;
	private MyAdapter listAdapter = null;
	
	private ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        myInsert = (Button)findViewById(R.id.myInsert);
        myQuery = (Button)findViewById(R.id.myQuery);
        myInsert.setOnClickListener(new InsertListents());
        myQuery.setOnClickListener(new QueryListents());
    }
    
    class InsertListents implements OnClickListener{
		@Override
		public void onClick(View v) {
			System.out.println("InsertListents-->");
			ContentValues values = new ContentValues();
			values.put(FirstProviderMetaData.UserTableMetaData.USER_NAME, "school");
			Uri uri = getContentResolver().insert(FirstProviderMetaData.UserTableMetaData.CONTENT_URI, values);
			System.out.println("uri-->"+uri.toString());
			resetList();
		}
    }
    
    class QueryListents implements OnClickListener{
		@Override
		public void onClick(View v) {
			Cursor c = getContentResolver().query(FirstProviderMetaData.UserTableMetaData.CONTENT_URI, null, null, null, null);
			while(c.moveToNext()){
				System.out.println("ID:"+c.getString(c.getColumnIndex(UserTableMetaData._ID))+"-->"+
						"NAME:"+c.getString(c.getColumnIndex(UserTableMetaData.USER_NAME)));
			}
		}
    }
    private void resetList() {		
    	
			list.clear();
			
			Cursor cursor = getContentResolver().query(FirstProviderMetaData.UserTableMetaData.CONTENT_URI, null, null, null, null);
			while(cursor.moveToNext()){
				String id = cursor.getString(cursor.getColumnIndex(UserTableMetaData._ID));
				String name = cursor.getString(cursor.getColumnIndex(UserTableMetaData.USER_NAME));
				System.out.println("query.id-->"+id+"--**--query.name-->"+name);
				
				HashMap<String, String> map = new HashMap<String, String>();
				map.put("user_ip", cursor.getString(cursor.getColumnIndex(UserTableMetaData._ID)));
				map.put("user_name", cursor.getString(cursor.getColumnIndex(UserTableMetaData.USER_NAME)));
				list.add(map);
				
			}

			if(listAdapter != null){
			if(listAdapter.getClass().equals(MyAdapter.class)){
				listAdapter.notifyDataSetChanged();
				}else{
					listAdapter.getClass().toString();
				}
			}else{
				listAdapter =new MyAdapter(Gan_TestCP.this, list, 
						R.layout.user, new String[] { "user_name", "user_ip" }, 
						new int[]{R.id.user_name_1,R.id.user_ip_1});
				setListAdapter(listAdapter);
			}
    }
}